package pers.amos.edu.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.amos.edu.common.GlobalResult;
import pers.amos.edu.service.UserService;

/**
 * @author amos wong
 * @create 2020-02-01 19:09
 */

@RestController
@RequestMapping("/edu/user")
@Api(tags = "用户接口")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录的接口 直接套用树洞的登录即可
     *
     * @param encryptedData
     * @param iv
     * @param code
     * @return
     */
    @ApiOperation("用户登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "encryptedData"), @ApiImplicitParam(name = "iv"), @ApiImplicitParam(name = "code")})
    @PostMapping("/login")
    public GlobalResult decodeUserInfo(String encryptedData, String iv, String code) {
        return userService.decodeUserInfo(encryptedData, iv, code);
    }

    /**
     * 保存用户手机号的接口
     *
     * @param openId 用户的openId
     * @return
     */
    @PostMapping("/phone")
    public GlobalResult getUserPhone(String openId, String encryptedData, String sessionKey, String iv) {
        return userService.saveUserPhone(openId, encryptedData, sessionKey, iv);
    }

    @ApiOperation("根据code获取用户openId的接口")
    @ApiImplicitParam(name = "code", value = "code")
    @GetMapping("/openId")
    public GlobalResult getUserOpenId(String code) {
        String openId = userService.getUserOpenId(code);
        return new GlobalResult(200, "获取openId成功", openId);
    }




}
